var vapp = new Vue({
	el: "#app",
	data: {
		show_header: false,
		show_bar : true,
		treeRootId : '0',// 树根节点
		curTreeId : '',// 监控树的节点id
		curTreeLevel : '',// 当前树的层级
		expandIds:[],
		deptName:'',
		defaultProps : {},
		treelist : [],
//		filterText: '',
		deptInfo: [],
		pName: '',
		pId: '',
		right: {
		  "add": false,
		  "edit": false,
		  "del": false
		},
		deptExec: '',
		deptForm: false,
		deptOrder: '',
		deptRemarks: '',
		kpJoin: '',
		taskNum: '',
		dutyName: '',
		dutyPhone: '',
		totalTask:'',
		form: {
		   name:'',
		   order:'',
		   desc:'',
//		   kp: '',
//		   task:'',
		   dname:'',
		   dphone:'',
		   total:'',		
		   
		   rules:{
//			   name: [
//			        { required: true, message: '请输入部门名称', trigger: 'blur' }
//	           ],
//	           kp:[
//	               { required: true }
//	           ],
//	           task: [
//					{validator:function(rule,value,callback){
//						var regPos = /^\d+$/;
//						if (value!=""&&!regPos.test(value)) {
//							callback(new Error('任务数必须为正整数'));
//						}else if(Number(value)<Number(vapp.$data.form.total)){
//							callback(new Error('任务数不能小于部门人员任务总数'+vapp.$data.form.total));
//						}else{
//							callback();
//						}
//						
//					}, trigger: 'change'}
//			   ],
	           order: [
					{validator:function(rule,value,callback){
						var regPos = /^-?\d+$/;
						if (value!=""&&!regPos.test(value)) {
							callback(new Error('显示顺序必须为整数'));
						}else{
							callback();
						}
						
					}, trigger: 'change'}
		       ],
		       desc: []
		   }
		},		
		exec: ''
	},
	
	
	created: function(){
		tools.loadDiv();
	},
	mounted: function(){
		var that = this;
		user.getUserInfo(function(data) {	
			tools.checkPriv(data.userinfo.privNames,"dept_manage");
			that.treeRootId = data.userinfo.curDeptId;
			
			that.curTreeId = data.userinfo.curDeptId;
			that.curTreeLevel = data.userinfo.curDeptLevel;
			that.deptName=data.userinfo.curDeptName;	
			
			that.getTreeList();
			that.getDeptInfo();
			
		});
		
	},
	computed:{
		
	},
	methods: {
		toggle_bar : function() {
			if (this.show_bar) {
				this.show_bar = false;
			} else {
				this.show_bar = true;
			}
		},
		
		getTreeList : function() {
			var defaultProps = {
				children : 'children',
				label : 'label',
				id : 'id'
			};
			this.defaultProps = defaultProps;

			var reqData = {
				"id" : this.treeRootId,
			};
			data = tools.ajaxRequest("../dept/deptTreeEx",
					reqData);
			if(data.respCode=="0"){
				this.treelist = data.tree;
				this.$nextTick(() => {
					 this.expandIds =[];
					 this.expandIds.push(this.curTreeId==''?this.treelist[0].id:this.curTreeId);
					 this.$refs.tree.setCurrentKey(this.curTreeId==''?this.treelist[0].id:this.curTreeId); 
				});
			}else{
				this.showError(data.respDesc);
				return false;
			}		
			
		},
		
		filterNode : function(value, data) {
			// alert(value);
			if (!value)
				return true;
			return data.label.indexOf(value) !== -1;
		},	
		
		handleNodeClick : function(data) {
			 //alert(JSON.stringify(data));
			// alert(data.id+"----"+data.deptLevel);
			this.deptName=data.label;
			this.curTreeId = data.id;
			this.curTreeLevel = data.deptLevel;
			this.getDeptInfo();
		},		
		
		
		getDeptInfo: function(){
			var that = this;
			var reqData ={
				"deptId" : this.curTreeId,
				"deptLevel": this.curTreeLevel
			};
			tools.ajaxRequestAsync("../dept/deptMain",reqData,function(data){
				that.reset();
				if (data.respCode != 0){
					that.showError("获取部门信息失败");
					return false;
				}else{
					that.deptInfo.push({
						"name": "部门名称",
						"value": data.deptName
					});

					if(data.kpJoin==""||data.kpJoin=="0"){
						that.deptInfo.push({
							"name": "参评",
							"value": "否"
						});
					}else if(data.kpJoin=="1"){
						that.deptInfo.push({
							"name": "参评",
							"value": "是"
						});
					}
					that.deptInfo.push({
						"name": "部门任务数",
						"value": data.taskNum
					});
					that.deptInfo.push({
						"name": "部门负责人",
						"value": data.dutyName
					});
					that.deptInfo.push({
						"name": "负责人联系电话",
						"value": data.dutyPhone
					});
					that.deptInfo.push({
						"name": "显示顺序",
						"value": data.deptOrder
					});
					that.deptInfo.push({
						"name": "部门描述",
						"value": data.deptRemarks
					});
					
					that.pId = data.deptParentId;
					that.pName =  data.deptParentName;
					
					that.deptName = data.deptName;
					that.deptOrder= data.deptOrder;
					that.deptRemarks= data.deptRemarks;
					that.kpJoin = data.kpJoin;
					that.taskNum = data.taskNum;
					that.dutyName = data.dutyName;
					that.dutyPhone = data.dutyPhone;
					that.totalTask = data.totalTask;
					
					if (data.deptAdd != undefined && data.deptAdd.show == "1") {
						that.right.add = true;
					}
					if (data.deptEdit != undefined && data.deptEdit.show == "1") {
						that.right.edit = true;
					}
					if (data.deptDel != undefined && data.deptDel.show == "1") {
						that.right.del = true;
					}
				}			
			});			
			
		},
		
		reset: function(){
			this.pName = '';
			this.pId = '';
			this.deptInfo = [];	
			this.right = {
				  "add": false,
				  "edit": false,
				  "del": false
			};
		},
		
		
		mdfDept: function(exec){
			var that = this;
			if(exec=="add"){
				this.deptExec = "部门添加";
				this.deptForm = true;
		//		this.$refs['form'].resetFields();
				this.form.name = '';
				this.form.order = '';
				this.form.desc = '';
//				this.form.kp = '0';
//				this.form.task = '';
				this.form.dname = '';
				this.form.dphone = '';
				this.form.total='';
				this.exec = "add";
			}else if(exec=="edit"){
				this.deptExec = "部门编辑";
				this.deptForm = true;
		//		this.$refs['form'].resetFields();
				this.form.name = this.deptName;
				this.form.order = this.deptOrder;
				this.form.desc = this.deptRemarks;
//				this.form.kp = this.kpJoin!="1"?"0":"1";
//				this.form.task = this.taskNum;
				this.form.dname = this.dutyName;
				this.form.dphone =this.dutyPhone;
				this.form.total=this.totalTask;
				
				this.exec = "edit";
			}else if(exec=="del"){
				this.$confirm('此操作将删除部门 【'+this.deptName+'】 , 是否继续?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(() => {
			         
			          var reqData = {
			        	 "deptId" : that.curTreeId
			          };
			          tools.ajaxRequestAsync("../dept/deptDel",reqData,function(data){
			        	  if(data.respCode=="0"){
			        		  that.$message({
						        	showClose: true,
						            type: 'success',
						            message: '删除成功!'
						      });
			        		  
			        		  that.curTreeId = data.deptId;
			        		  that.curTreeLevel = data.deptLevel;
			        		  that.getTreeList();		
			        		  that.getDeptInfo();
			        		  
			        	  }else{
			        		  that.showError("删除失败");
			        	  }			        	  
			          });
			          
			          
			        }).catch(() => {        
			    });
			}				
			
		},		
		
		close:function(){
			this.$refs['form'].resetFields();
		},
		
		save: function(){
			var that = this;
			var reqData = {};
			this.$refs['form'].validate((valid) => {
		          if (valid) {
		        	  if(this.form.name.trim()==""){
		        		  that.showError("部门名称不能为空");
		        		  return false;
		        	  }
		        	  if(this.form.name.length<2||this.form.name.length>30){
		        		  that.showError("部门名称长度在2到30个字符");
		        		  return false;
		        	  }
		        	  if(this.exec=="add"){
		  				reqData = {
		  					"deptId" : "",
		  					"deptName" : this.form.name,
		  					"remarks" : this.form.desc,
		  					"deptOrder" : this.form.order.toString(),
		  					"deptParentId" : this.curTreeId,
		  					"deptLevel": this.curTreeLevel,
		  					"kpJoin": "0",
		  					"taskNum": "0"
		  				};
		  				
		  				this.deptForm = true;
		  			}else if(this.exec=="edit"){
		  				reqData = {
		  					"deptId" : this.curTreeId,
		  					"deptName" : this.form.name,
		  					"remarks" : this.form.desc,
		  					"deptOrder" : this.form.order.toString(),
		  					"deptParentId" : this.pId,
		  					"deptLevel": this.curTreeLevel,
		  					"kpJoin": "0",
		  					"taskNum": "0"
		  				};
		  				if(reqData.kpJoin!=this.kpJoin||reqData.taskNum!=this.taskNum) {
		  					reqData.stsUpdate = "true";
		  				}else{
		  					reqData.stsUpdate = "false";
		  				}
		  			}		  			
		        	
		  			tools.ajaxRequestAsync("../dept/deptSave", reqData, function(data){
		  				if(data.respCode=="0"){
		  					that.$message({
		  						  showClose: true,
		  				          message: '保存成功',
		  				          type: 'success'
		  				    });
		  					that.deptExec = '';
		  					that.deptForm = false;
		  					that.curTreeId = data.deptId;
		  					that.curTreeLevel = data.deptLevel;
		  					that.getTreeList();
		  					that.getDeptInfo();
		  					that.$refs['form'].resetFields();
		  				}else{
		  					that.showError(data.respDesc);
		  					return false;
		  				}
		  			});
		  			
		          } else {
		            return false;
		          }
		    });
			
			
		},
		
		cancel: function(){
			this.deptExec = '';
			this.deptForm = false;
			this.$refs['form'].resetFields();
		},
		
		
		showError: function(info){
			var i = (info==undefined?"加载数据错误":info);
			this.$alert(i, '系统错误', {
		          confirmButtonText: '确定'
		    });	
		}
		
	},
	watch:{
//		filterText : function() {
//			this.$refs.tree.filter(this.filterText);
//			// alert(this.filterText);
//		},
		
		curTreeId : function() {
			// alert(this.currentPageNbr);
			//this.getDeptInfo();
		}
		
	}
	
});